import router, { routerPaths } from './router'
import { getToken } from '@/utils/auth'
const { login, home } = routerPaths
const whiteList = [login, '/']
import userStore from '@/store/user'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
NProgress.configure({ showSpinner: false })

router.beforeEach(async (to, _from, next) => {
  NProgress.start()
  // 没有登录
  if (!getToken()) {
    // 跳转到登录页面
    to.path === login ? next() : next({ path: login })
    return
  }
  // 如果登录了，判断路径在表名单中，就重定向
  if (whiteList.some((v) => v === to.path)) {
    next({ path: home })
    return
  }
  const u = userStore.getters.hasUserInfo
  console.log('hasUserInfo', u)
  // 判断有无用户信息
  if (!u) {
    // 获取用户信息
    await userStore.dispatch('getInfo')
  }
  next()
  NProgress.done()
})

router.afterEach(() => NProgress.done())